﻿<!DOCTYPE>

<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
    <title>文件管理器</title>
    <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
   
</head>
<body>
    <div class="panelSection">    
        <div class="easyui-panel " title="操作按钮区" style="padding:3px;" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
               <div style="margin: 3px 0px 3px 3px">   
                           
                    <button  id="btnPrevPath" class="easyui-linkbutton" data-options="width:100,iconCls:'icon-back'" title="回到上一级文件夹" 
                    style="float: left;margin-top:17px;margin-left:0px">上一级</button>

                    <div style="float: left; margin-left: 0px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            顶层路径：</div>
                        <input id="txtTopPath" class="easyui-textbox" name="ProductNo" type="text" data-options=""
                            style="width: 600px; height: 22px" />
                    </div>

                    <button  id="btnTargetPath" class="easyui-linkbutton" data-options="width:100,iconCls:'icon-search'" title="到指定路径下" style="margin-top:17px">
                        确定</button>
                    <button  id="btnDown" class="easyui-linkbutton" data-options="width:100,iconCls:'icon-hamburg-down'" title="下载文件到本地" style="margin-top:17px;margin-left:20px">
                        下载</button>
               </div> 

        </div> 

    </div> 
    <div class="panelSection">    
        <div class="easyui-panel " title="展示区域" style="padding:3px;" 
        data-options="closable:false,collapsible:true,minimizable:false,maximizable:false"> 
            <div style="margin: 3px 0px 3px 3px">
                <table id="gdFiles" class="easyui-datagrid" style="width: 100%; height: 600"
                        data-options="rownumbers:true,pagination:false,striped:false,checkOnSelect:true,selectOnCheck:true,
                        singleSelect:false,multiSort:true,remoteSort:false,pageNumber:1,pageSize:5,pageList:[5,10,20]">
                    
                </table>  
            </div>
        </div>
    </div>

    <div style="display:none">
         <input id="hdPrevPath" type="text" name="name" value="" />
    </div>
    <div id="tabsMenu" class="easyui-menu" style="width:150px;">
         <div data-options="name:'down',iconCls:'icon-hamburg-down'">下载</div>
         <div data-options="name:'copy'">复制</div>
         <div data-options="name:'cut',iconCls:'icon-cut'">剪切</div>
         <div data-options="name:'paste'">粘贴</div>
         <div data-options="name:'rename'">重命名</div>
         <div data-options="name:'del',iconCls:'icon-no'">删除</div>
    </div>
</body>
</html>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
    var page = {
        rootPath: getRootPath(), //网站根路径
        ajaxUrl: getRootPath() + "/Ashx/BrowseFilesManagerHandler.ashx",
        //初始化
        init: function () {
            page.bind();
        },
        bind: function () {
            //右键菜单
            $('#tabsMenu').menu({
                onClick: function (item) {
                    if (item.name == "down") {
                        page.downDirOrFiles();
                    }
                    if (item.name == "del") {
                        $.messager.confirm({
                            title: '警告',
			                msg: '你即将删除所选择的文件夹/文件，确认删除？？？',
			                ok: "删除",
			                cancel: "取消",			  
			                fn : function(r){
                                if(r){				                    
                                    page.deleteDirAndFiles();                           
                                } 
			                }
                        });
                    }
                }
            })

            $("#gdFiles").datagrid({
                columns: [[
                    { field: 'ck', title: '选择', checkbox: true },
                    { field: 'fileName', title: '名称', sortable: true, formatter: function (value, row, index) {
                        if (row.type == "文件夹") {
                            //return '<img width="20" height="20" style="opacity:0" class="icon-hamburg-folder"></img> ' + value
                            return '<span style="background-position-x:left;display:inline-block;padding-left:20px;height:20px;line-height:16px" class="icon-hamburg-folder">' + value + '</span>'
                        } else {
                            return '<span style="background-position-x:left;display:inline-block;padding-left:20px;height:20px;line-height:16px" class="icon-hamburg-docs">' + value + '</span>';
                        }
                    }
                    },
                    { field: 'lastChangeDate', title: '修改日期', width: 130, sortable: true },
                    { field: 'type', title: '类型', width: 100, sortable: true },
                    { field: 'size', title: '大小(KB)', width: 100, sortable: true }
                ]],
                onDblClickRow: function (rowIndex, rowData) {
                    if (rowData.type == "文件夹") {
                        var topPath = $("#txtTopPath").textbox('getValue').trim("\\\\").trim("/");
                        page.showFilesByDir(topPath + "\\" + rowData.fileName);
                    }
                },
                onRowContextMenu: function (e, rowIndex, rowData) {
                    e.preventDefault();
                    $('#tabsMenu').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    }).data("tabTitle", "");

                    //$("#gdFiles").datagrid("uncheckAll");
                    $("#gdFiles").datagrid("checkRow", rowIndex);
                }
            });
            //查看顶层路径下的文件夹/文件
            $('#btnTargetPath').on("click", function () {
                page.showFilesByDir($("#txtTopPath").textbox('getValue'));

            });
            //查看上一层
            $('#btnPrevPath').on("click", function () {
                page.showFilesByDir($("#hdPrevPath").val());
            });
            //下载数据
            $('#btnDown').on("click", function () {
                page.downDirOrFiles();
            });
        },
        //显示指定路径下的文件/文件夹
        showFilesByDir: function (topPath) {
            topPath = topPath.trim("\\\\").trim("/");
            topPath += "\\";
            var gridID = "#gdFiles";
            var param = {
                action: "showFilesByDir",
                topPath: topPath
            }
            if (isNullOrWhiteSpaceStr(param.topPath)) {
                displayMessage("请输入必填项", false);
                return;
            }

            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $(gridID).datagrid('loading');
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    //console.log(data);
                    $(gridID).datagrid('loaded');
                    $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) {
                        $(gridID).datagrid('loadData', { total: result.ReturnData.length, rows: result.ReturnData });

                        $("#txtTopPath").textbox('setValue', topPath)
                        $("#hdPrevPath").val(page.getLastDir(topPath));
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $(gridID).datagrid('loaded');
                    $(this).removeAttr("disabled"); //恢复按钮
                    alert(XMLHttpRequest.responseText);
                }
            });


        },
        //获取上一级路径
        getLastDir: function (thisTopPath) {
            thisTopPath = thisTopPath.trim("\\\\").trim("/");
            var index = thisTopPath.lastIndexOf("/");
            if (index == -1) { index = thisTopPath.lastIndexOf("\\") }
            var prevPath = "";
            if (index == -1) { prevPath = thisTopPath }
            else { prevPath = thisTopPath.substring(0, index) }
            return prevPath;
        },
         //删除文件
        deleteDirAndFiles:async function () {
            var selectData = $("#gdFiles").datagrid("getChecked");
            var isPost=true;//是否提交post
            if (selectData.length == 0) {
                displayMessage("请选择记录", false);
                return;
            }
            if (selectData.find(function (item) { return item.type == "文件夹" })) {
                await new Promise((resolve, reject) => {
                    $.messager.confirm({
                        title: '提示',
			            msg: '删除的包含文件夹，将删除文件夹及包含的所有文件/文件夹,请再次确认！！！',
			            ok: "删除",
			            cancel: "取消",			  
			            fn : function(r){
                            if(!r){
				                isPost=false;                                
                            }
                            resolve();
			            }
                    });
                });
            }
            if(!isPost)
                return;
            var param = {
                action: "deleteDirAndFiles",
                topPath: $("#txtTopPath").textbox('getValue'),
                dirOrFiles: JSON.stringify(selectData)
            }

            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) {
                        page.showFilesByDir($("#txtTopPath").textbox('getValue'));
                        displayMessage(result.Message, true);

                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $(this).removeAttr("disabled"); //恢复按钮
                    alert(XMLHttpRequest.responseText);
                }
            });

        },
        //下载数据
        downDirOrFiles: function () {
            var selectData = $("#gdFiles").datagrid("getChecked");
            if (selectData.length == 0) {
                displayMessage("请选择记录", false);
                return;
            }
            var param = {
                action: "downDirOrFiles",
                topPath: $("#txtTopPath").textbox('getValue'),
                dirOrFiles: JSON.stringify(selectData)
            }
            //console.log(selectData);

            $.ajax({
                type: "Post",
                url: page.ajaxUrl,
                data: param,
                cashe: false,
                async: true,
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    $.messager.progress('close');
                    var result = JSON.parse(data);
                    if (result.Result) {
                        displayMessage(result.Message, true);
                        //下载文件
                        window.open(page.rootPath + result.ReturnData);
                    } else {
                        displayMessage(result.Message, false);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $(this).removeAttr("disabled"); //恢复按钮
                    alert(XMLHttpRequest.responseText);
                }
            });
        }
    }

    $(function () {
        page.init();
    });
</script>